package com.zxblog.controller;

import com.zxblog.domain.UserNormal;
import com.zxblog.service.NormalUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/controls")
public class AdminControlsController {

    @Autowired
    private NormalUserService normalUserService;

    @PostMapping("/normalUser/searchNormalUser")
    @ResponseBody
    public Object normalUserSearchNormalUser(@RequestBody Map<String,Object> requestParamMap){
        Map<String,Object> retMap = new LinkedHashMap<>();
        if (requestParamMap.get("userName") != null) { // 数据效验
            List<UserNormal> userInfoList = normalUserService.getUserInfoByName(requestParamMap.get("userName").toString());
            retMap.put("code",200);
            retMap.put("msg","数据获取成功");
            retMap.put("userName",requestParamMap.get("userName"));
            retMap.put("data",userInfoList);
        } else { // 效验失败
            retMap.put("code",500);
            retMap.put("msg","数据参数不完整");
            retMap.put("userName","参数不完整");
            retMap.put("data",null);
        }

        return retMap;
    }

    @PostMapping("/normalUser/addNormalUser")
    @ResponseBody
    public Object normalUserAddUser(@RequestBody Map<String,Object> requestParamMap){
        Map<String,Object> retMap = new LinkedHashMap<>();
        // 效验数据完整性
        if (requestParamMap.get("userName") != null && requestParamMap.get("password") != null && requestParamMap.get("sex") != null) {
            // 构造参数实体类
            UserNormal userNormal = new UserNormal();
            userNormal.setUsername(requestParamMap.get("userName").toString());
            userNormal.setPassword(requestParamMap.get("password").toString());
            userNormal.setGender(Integer.parseInt(requestParamMap.get("sex").toString()));
            // 调用Service方法
            userNormal = normalUserService.addUser(userNormal);

            retMap.put("code",200);
            retMap.put("msg","请求成功");
            retMap.put("insertedUser",userNormal);
        } else { // 完整性效验失败
            retMap.put("code",500);
            retMap.put("msg","数据参数不完整");
            retMap.put("insertedUser",null);
        }
        return retMap;
    }

    @PostMapping("/normalUser/getUserInfoByUid")
    @ResponseBody
    public Object getUserInfoByUid(@RequestBody Map<String,Object> requestBodyMap){
        Map<String,Object> retMap = new LinkedHashMap<>();
        if (requestBodyMap.get("uid") != null) {
            UserNormal searchedUser = normalUserService.getUserInfoByUid(Integer.parseInt(requestBodyMap.get("uid").toString()));
            retMap.put("code",200);
            if (searchedUser != null){
                retMap.put("msg","查询成功");
                retMap.put("userInfo",searchedUser);
            } else {
                retMap.put("msg","搜索为空");
            }
        } else {
            retMap.put("code", 500);
            retMap.put("msg", "数据参数不完整");
        }
        return retMap;
    }

}
